/*     */ package com.cr.framework.shiro.web.session;
/*     */ 
/*     */ import com.cr.common.utils.MyStringUtils;
/*     */ import com.cr.common.utils.bean.MyBeanUtils;
/*     */ import com.cr.common.utils.spring.SpringUtils;
/*     */ import com.cr.framework.shiro.session.OnlineSession;
/*     */ import com.cr.system.domain.SysUserOnline;
/*     */ import com.cr.system.service.ISysUserOnlineService;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Collection;
/*     */ import java.util.Date;
/*     */ import java.util.List;
/*     */ import org.apache.commons.lang3.time.DateUtils;
/*     */ import org.apache.shiro.session.InvalidSessionException;
/*     */ import org.apache.shiro.session.Session;
/*     */ import org.apache.shiro.session.mgt.DefaultSessionKey;
/*     */ import org.apache.shiro.session.mgt.SessionKey;
/*     */ import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
/*     */ import org.slf4j.Logger;
/*     */ import org.slf4j.LoggerFactory;
/*     */ 
/*     */ public class OnlineWebSessionManager extends DefaultWebSessionManager {
/*  31 */   private static final Logger log = LoggerFactory.getLogger(OnlineWebSessionManager.class);
/*     */   
/*     */   public void setAttribute(SessionKey sessionKey, Object attributeKey, Object value) throws InvalidSessionException {
/*  36 */     super.setAttribute(sessionKey, attributeKey, value);
/*  37 */     if (value != null && needMarkAttributeChanged(attributeKey)) {
/*  39 */       OnlineSession session = getOnlineSession(sessionKey);
/*  40 */       session.markAttributeChanged();
/*     */     } 
/*     */   }
/*     */   
/*     */   private boolean needMarkAttributeChanged(Object attributeKey) {
/*  46 */     if (attributeKey == null)
/*  48 */       return false; 
/*  50 */     String attributeKeyStr = attributeKey.toString();
/*  52 */     if (attributeKeyStr.startsWith("org.springframework"))
/*  54 */       return false; 
/*  56 */     if (attributeKeyStr.startsWith("javax.servlet"))
/*  58 */       return false; 
/*  60 */     if (attributeKeyStr.equals("username"))
/*  62 */       return false; 
/*  64 */     return true;
/*     */   }
/*     */   
/*     */   public Object removeAttribute(SessionKey sessionKey, Object attributeKey) throws InvalidSessionException {
/*  70 */     Object removed = super.removeAttribute(sessionKey, attributeKey);
/*  71 */     if (removed != null) {
/*  73 */       OnlineSession s = getOnlineSession(sessionKey);
/*  74 */       s.markAttributeChanged();
/*     */     } 
/*  77 */     return removed;
/*     */   }
/*     */   
/*     */   public OnlineSession getOnlineSession(SessionKey sessionKey) {
/*  82 */     OnlineSession session = null;
/*  83 */     Object obj = doGetSession(sessionKey);
/*  84 */     if (MyStringUtils.isNotNull(obj)) {
/*  86 */       session = new OnlineSession();
/*  87 */       MyBeanUtils.copyBeanProp(session, obj);
/*     */     } 
/*  89 */     return session;
/*     */   }
/*     */   
/*     */   public void validateSessions() {
/*  98 */     if (log.isInfoEnabled())
/* 100 */       log.info("invalidation sessions..."); 
/* 103 */     int invalidCount = 0;
/* 105 */     int timeout = (int)getGlobalSessionTimeout();
/* 106 */     if (timeout < 0)
/*     */       return; 
/* 111 */     Date expiredDate = DateUtils.addMilliseconds(new Date(), 0 - timeout);
/* 112 */     ISysUserOnlineService userOnlineService = (ISysUserOnlineService)SpringUtils.getBean(ISysUserOnlineService.class);
/* 113 */     List<SysUserOnline> userOnlineList = userOnlineService.selectOnlineByExpired(expiredDate);
/* 115 */     List<String> needOfflineIdList = new ArrayList<>();
/* 116 */     for (SysUserOnline userOnline : userOnlineList) {
/*     */       try {
/* 120 */         DefaultSessionKey defaultSessionKey = new DefaultSessionKey(userOnline.getSessionId());
/* 121 */         Session session = retrieveSession((SessionKey)defaultSessionKey);
/* 122 */         if (session != null)
/* 124 */           throw new InvalidSessionException(); 
/* 127 */       } catch (InvalidSessionException e) {
/* 129 */         if (log.isDebugEnabled()) {
/* 131 */           boolean expired = e instanceof org.apache.shiro.session.ExpiredSessionException;
/* 132 */           String msg = "Invalidated session with id [" + userOnline.getSessionId() + "]" + (expired ? " (expired)" : " (stopped)");
/* 134 */           log.debug(msg);
/*     */         } 
/* 136 */         invalidCount++;
/* 137 */         needOfflineIdList.add(userOnline.getSessionId());
/* 138 */         userOnlineService.removeUserCache(userOnline.getLoginName(), userOnline.getSessionId());
/*     */       } 
/*     */     } 
/* 142 */     if (needOfflineIdList.size() > 0)
/*     */       try {
/* 146 */         userOnlineService.batchDeleteOnline(needOfflineIdList);
/* 148 */       } catch (Exception e) {
/* 150 */         log.error("batch delete db session error.", e);
/*     */       }  
/* 154 */     if (log.isInfoEnabled()) {
/* 156 */       String msg = "Finished invalidation session.";
/* 157 */       if (invalidCount > 0) {
/* 159 */         msg = msg + " [" + invalidCount + "] sessions were stopped.";
/*     */       } else {
/* 163 */         msg = msg + " No sessions were stopped.";
/*     */       } 
/* 165 */       log.info(msg);
/*     */     } 
/*     */   }
/*     */   
/*     */   protected Collection<Session> getActiveSessions() {
/* 173 */     throw new UnsupportedOperationException("getActiveSessions method not supported");
/*     */   }
/*     */ }


/* Location:              D:\tmp\creditorRights-admin\BOOT-INF\lib\creditorRights-framework-4.7.0.jar!\com\cr\framework\shiro\web\session\OnlineWebSessionManager.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */